<?php include("includes/session.php"); ?>
<?php include("includes/connect_db.php"); ?>
<?php include("includes/header.php"); ?>
<?php include("notif.php"); ?>

<?php
	$id_draft = $_GET['id'];

	if ($id_draft == ""){
		$penerima = "";
		$pesan = "";
		$nomor = "";
	} else{
		$sql_cari_draft = "SELECT * FROM tb_draft WHERE id_draft='$id_draft'";
		$query_cari_draft = mysql_query($sql_cari_draft);
		while($cari_draft = mysql_fetch_array($query_cari_draft)){
			$penerima = $cari_draft['penerima'];
			$pesan = $cari_draft['pesan'];
			$id_draft = $cari_draft['id_draft'];
		}

		$sql_cek_nomor = "SELECT * FROM tb_kontak WHERE no_hp = '$penerima'";
		$query_cek_nomor = mysql_query($sql_cek_nomor);
		$hitung_row = mysql_num_rows($query_cek_nomor);

		if ($hitung_row == 0) {
			$nomor = $penerima;
		} else{
			$query_cek_nomor = mysql_query($sql_cek_nomor);
			$cek_nomor = mysql_fetch_array($cek_nomor);
			$nomor = $cek_nomor['no_hp'];
		}
	}
?>

<style>
	div{
		font-family: 'SEGOE UI';
	}
</style>

<div class="span9">  
    <h3>SMS</h3>                      
    <ul class="breadcrumb well">
    	<li><a href="draft.php">Draft</a> <span class="divider">/</span></li>
        <li class="active">View</li>
    </ul>    
    
    <div style="width: 100%">
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<div class="alert alert-info">
				SMS Baru
		    </div>
			<div class="input-prepend">
				<span class="add-on">Nomor</span>
				<input type="hidden" name="id_draft" value="<?php echo $id_draft ?>">
				<input type="hidden" name="nomor" value="<?php echo $nomor ?>">
				<input type="text" name="penerima" style="width:90%" value="<?php echo $penerima ?>">
				<span class="add-on"><i class="icon-user"></i></span>
			</div>
			<div><b>Text</b></div>
			<div class="control-group">
				<textarea name="pesan" style="width: 100%; height: 200px; overflow: hidden; word-wrap: break-word; resize: none;"><?php echo $pesan ?></textarea>
			</div>
			<div class="control-group">
				<div class="controls">
					<button type="submit" style="width: 80px;" class="btn btn-info" name="kirim">Kirim</button>
					<button type="submit" style="width: 80px;" class="btn" name="simpan">Simpan</button>
					<button type="submit" style="width: 80px;" class="btn btn-danger" name="batal">Batal</button>
				</div>
			</div>
		</div>
		</form>
	</div>

	<?php
		$penerima = $_POST['penerima'];
		$nomor = $_POST['nomor'];
		$pesan = $_POST['pesan'];
		$id_draft = $_POST['id_draft'];
		$waktu = 'CONCAT(CURDATE(), " ", CURTIME())';
		
		if (isset($_POST['kirim'])){
			$sql_input_outbox = "INSERT INTO outbox 
										(SendingDateTime, DestinationNumber,TextDecoded, Text, UDH, CreatorID) 
									VALUES 
										($waktu, '$nomor', '$pesan', '', '', '')";
				$query_input_outbox = mysql_query($sql_input_outbox) or die(mysql_error());

				if ($query_input_outbox) {
					$sql_hapus_draft = "DELETE FROM tb_draft WHERE id_draft = '$id_draft'";
					$query_hapus_draft = mysql_query($sql_hapus_draft) or die(mysql_error());

					echo "<SCRIPT> alert('Pesan Telah Dikirim'); location.href='inbox.php';</SCRIPT>";
				} else {
					echo "<SCRIPT> alert('Data Gagal Disimpan');</SCRIPT>";
				}
		} elseif (isset($_POST['simpan'])) {
			$sql_hapus_draft = "DELETE FROM tb_draft WHERE id_draft = '$id_draft'";
			$query_hapus_draft = mysql_query($sql_hapus_draft) or die(mysql_error());

			$sql_simpan_pesan = "INSERT INTO tb_draft (penerima, pesan, waktu) Value ('$penerima', '$pesan', $waktu)";
			$query_simpan_pesan = mysql_query($sql_simpan_pesan) or die(mysql_error());

			if ($query_simpan_pesan) {
				echo "<SCRIPT> alert('Pesan Disimpan Pada Draft'); location.href='draft.php';</SCRIPT>";
			} else {
				echo "<SCRIPT> alert('Data Gagal Disimpan');</SCRIPT>";
			}
		} elseif (isset($_POST['batal'])) {
			echo "<SCRIPT> location.href='draft.php';</SCRIPT>";
		}
	?>
    	

</div><!--/span-->

<?php include("includes/footer.php"); ?>

<script type="text/javascript">
	
</script>